/*
@title: Astroids
@description: Astroids is a real-time action game where the player must avoid incoming asteroids using WASD controls. The objective is to maneuver the player's sprite through a field of randomly appearing asteroids, preventing collisions to survive as long as possible. The game challenges players with increasing time pressure as they navigate through obstacles.
@author: Alex
@tags: ['real-time','action']
@addedOn: 2024-08-17
*/
let timeCounter = 0;
let playerControlsEnabled = true;
let player = "p";
const asteroid = "a";
let playerUp = "1";
let playerDown = "2";
let playerLeft = "3";
let playerRight = "4";
const background = "9";
const death = "b";

setLegend(
  [player, bitmap`
.......12.......
......1122......
.....11LL22.....
.....1LLLL2.....
.....1LLLL2.....
.....1LLLL2.....
.....111222.....
.....111272.....
.....111222.....
....51112227....
...5511122277...
...5511122277...
...5511122277...
...55.9369.77...
.......66.......
.......9........`],
  [playerUp, bitmap`
.......12.......
......1122......
.....11LL22.....
.....1LLLL2.....
.....1LLLL2.....
.....1LLLL2.....
.....111222.....
.....111272.....
.....111222.....
....51112227....
...5511122277...
...5511122277...
...5511122277...
...55.9369.77...
.......66.......
.......9........`],
  [playerRight, bitmap`
................
................
................
..5555..........
..55555.........
...11111111111..
..91111111LLL11.
9631111111LLLL11
.662222222LLLL22
..92222272LLL22.
...22222222222..
..77777.........
..7777..........
................
................
................`],
  [playerDown, bitmap`
.......99.......
.......66.......
...77.9639.55...
...7722211155...
...7722211155...
...7722211155...
....72221115....
.....222111.....
.....272111.....
.....222111.....
.....2LLLL1.....
.....2LLLL1.....
.....2LLLL1.....
.....22LL11.....
......2211......
.......21.......`],
  [playerLeft, bitmap`
................
................
................
..........7777..
.........77777..
..22222222222...
.22LLL27222229..
22LLLL222222266.
11LLLL1111111369
.11LLL11111119..
..11111111111...
.........55555..
..........5555..
................
................
................`],
  [asteroid,bitmap`
................
................
.....LLLLLL.....
...LLLLL000LL...
...LLLLL000LL...
..LLLLLLLL1LLL..
..L111LLLLLLLL..
..L11100LLLLLL..
..L11100111LLL..
..LLLLLL111LLL..
..LLLLLL111LLL..
...LL00LLLLLL...
...LL00LLLLLL...
.....LLLLLL.....
................
................`],
  [background,bitmap`
0000000000000002
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0020000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000200
0000000000000000`],
  [background,bitmap`
0000000000000000
0000000000000000
0002000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000002000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000020000000000
0000000000000000
0000000000000000`],
  [death,`..6.............
......66..66...6
.....6966.36.6..
..966..9...666..
..6..699.999.66.
.6..6.9.9.3..96.
.69699.9633966.6
..6.9393.363.966
.66.96..3.39..36
6.3669.93.93.6.6
..6.....696.96..
...6.699.99..66.
..66.69.66.6.6.6
....966.6.3.....
.......6.6......
..6..........6..`],
);

let level = 0;
const levels = [
  map`
...........
...........
...........
...........
...........
...........
.....1.....
...........
...........`
]

setBackground(background);


setMap(levels[level]);

let currentPlayerState = playerUp;
function switchPlayerSprite(changeSprite) {
  const p = getFirst(currentPlayerState);
  clearTile(p.x, p.y);
  addSprite(p.x, p.y, changeSprite);
  currentPlayerState = changeSprite;
}


onInput("w", () => {
   if (playerControlsEnabled){
  switchPlayerSprite(playerUp);
  getFirst(currentPlayerState).y -= 1}
});

onInput("s", () => { 
  if (playerControlsEnabled){
  switchPlayerSprite(playerDown);
  getFirst(currentPlayerState).y += 1}
});

onInput("d", () => {
   if (playerControlsEnabled){
  switchPlayerSprite(playerRight);
  getFirst(currentPlayerState).x += 1}
});

onInput("a", () => {
   if (playerControlsEnabled){
  switchPlayerSprite(playerLeft);
  getFirst(currentPlayerState).x -= 1}
});

const interval = setInterval(() => {
  timeCounter++;
  // Update the displayed time on the screen
  addText(`Time: ${timeCounter}`, { x: 1, y: 1, color: color`2` });
}, 1000); 

const interval2 = setInterval(() => {
  const random = Math.random()
  if (random > 0.4) {
    const randomx = Math.floor(Math.random() * 8)
    addSprite(randomx, 0, asteroid)
  }
  const asteroids = getAll(asteroid)
  asteroids.forEach((a) => {
    detectCollision()
    if (a.y > 7){
      clearTile(a.x, a.y)
    }
    a.y++
  })
  
}, 600); 



const music = tune`
508.47457627118644: A4-508.47457627118644 + G4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4-508.47457627118644 + G4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4-508.47457627118644 + G4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4-508.47457627118644 + G4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4-508.47457627118644 + G4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: G4/508.47457627118644 + A4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644 + B4-508.47457627118644 + A4/508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4/508.47457627118644 + B4-508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644 + C5-508.47457627118644 + B4/508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C5-508.47457627118644 + B4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644 + B4-508.47457627118644 + A4/508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: B4-508.47457627118644 + A4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644: A4-508.47457627118644 + G4/508.47457627118644 + C4~508.47457627118644 + D4^508.47457627118644 + E4^508.47457627118644,
508.47457627118644`
playTune(music, Infinity);

const deathsound = tune`
37.5: B5-37.5 + A5-37.5 + G5-37.5 + F5/37.5 + E5/37.5,
37.5: G5-37.5 + F5-37.5 + E5-37.5 + D5-37.5,
37.5: E5-37.5 + D5-37.5 + C5-37.5 + B4-37.5,
37.5: C5/37.5 + B4/37.5 + A4-37.5 + D5~37.5,
37.5: G4/37.5 + A4/37.5 + B4~37.5 + C5~37.5,
37.5: G4^37.5 + F4^37.5 + B4~37.5 + A4~37.5,
37.5: F4^37.5 + E4^37.5 + G4-37.5 + D4~37.5 + A4~37.5,
37.5: E4^37.5 + D4~37.5,
37.5: E4^37.5 + D4~37.5,
37.5: E4^37.5 + D4^37.5 + C4~37.5,
37.5: D4^37.5 + E4-37.5 + C4~37.5,
37.5: D4^37.5 + C4~37.5,
37.5: D4^37.5 + C4~37.5,
37.5: D4^37.5 + C4~37.5,
37.5: C4-37.5 + D4-37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
37.5: C4^37.5,
112.5`

  const playerTile = getTile(getFirst(currentPlayerState).x, getFirst(currentPlayerState).y)[0];



console.log(playerTile)
  


function detectCollision() {
  if (tilesWith(currentPlayerState, asteroid).length == 1) {
    // if (tilesWith(asteroid).some(tile => tile.x === playerTile.x && tile.y === playerTile.y)) {
    playerControlsEnabled = false;
    addText("Game Over", { x: 6, y: 5, color: color`3` });
    addText("---- ----", { x: 6, y: 6, color: color`3` });
    switchPlayerSprite(death);
    clearInterval(interval)
    playTune(death);
    // }
  }
}

afterInput(() => {
  detectCollision();
});d
